package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.ScenicDetail;
import cn.wolfcode.luowowo.article.domain.ScenicTag;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicDetailService;
import cn.wolfcode.luowowo.article.service.IScenicTagService;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.domain.ScenicCommentReply;
import cn.wolfcode.luowowo.comment.service.IScenicCommentReplyService;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.comment.vo.SumResult;
import cn.wolfcode.luowowo.common.query.ScenicQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("scenic")
public class ScenicController {

    @Value("${file.dir}")
    private String dir;

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicDetailService scenicDetailService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @Reference
    private IScenicCommentReplyService replyService;
    @Reference
    private IScenicTagService scenicTagService;

    @RequestMapping("seeMore")
    public String seeMore(Model model, Long scenicId, int page){
        List<ScenicDetail> chils = scenicDetailService.getChilPage(scenicId,page);
        model.addAttribute("chils",chils);
        model.addAttribute("page",page);
        return "scenic/seeMore";
    }

    @RequestMapping("overgo")
    @ResponseBody
    public Object overgo(Long scenicId, @LoginUser UserInfo userInfo){
        AjaxResult result = new AjaxResult();
        //先判断用户是否登录
        if (userInfo == null) {
            result.mark("请先登录");
            return result;
        }
        result = scenicDetailService.overgo(scenicId);
        return result;
    }

    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long scenicId, @LoginUser UserInfo userInfo){
        AjaxResult result = new AjaxResult();
        //先判断用户是否登录
        if (userInfo == null) {
            result.mark("请先登录");
            return result;
        }
        result = scenicDetailService.favor(scenicId,userInfo.getId());
        return result;
    }

    @RequestMapping("list")
    public String list(Long destId, Model model) {
        //景点标签
        List<ScenicTag> tags = scenicTagService.selectAll();
        model.addAttribute("tags",tags);

        //热门景点, 查询 "去过" 数量前十的
        List<ScenicDetail> hotScenics = scenicDetailService.listHotScenic(destId);
        System.out.println(hotScenics.size());
        model.addAttribute("hotScenics",hotScenics);

        //点评量最多的三个景点
        List<ScenicDetail> scenics = scenicDetailService.listScenicTop3(destId);
        model.addAttribute("scenics", scenics);

        //拿到吐司数据,显示到页面上
        List<Destination> toasts = destinationService.getToasts(destId);
        // 吐司toasts
        model.addAttribute("toasts", toasts);
        // 目的地dest
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        return "scenic/list";
    }

    @RequestMapping("/scenicPage")
    public String scenicPage(@ModelAttribute("qo") ScenicQueryObject qo, Model model){
        PageInfo<ScenicDetail> pageInfo = scenicDetailService.query(qo);
        model.addAttribute("page",pageInfo);
        return "scenic/scenicPage";
    }

    @RequestMapping("detail")
    public String detail(Long scenicId, Long destId, Model model, Integer pageNum) {

        //查询当前景点下的评论统计数据
        SumResult vo = scenicCommentService.getSumData(scenicId);
        model.addAttribute("vo", vo);

        //拿到吐司数据,显示到页面上
        List<Destination> toasts = destinationService.getToasts(destId);
        // 吐司toasts
        model.addAttribute("toasts", toasts);
        // 目的地dest
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));

        //根据景点id查询景点的相关信息
        ScenicDetail scenic = scenicDetailService.getById(scenicId);
        model.addAttribute("scenic", scenic);

        List<ScenicDetail> children = scenic.getChildren();
        if(children.size() >= 4){
            model.addAttribute("children", children.subList(0,4));
        }else {
            model.addAttribute("children", children);
        }

        return "scenic/detail";
    }

    @RequestMapping("files")
    @ResponseBody
    public String files(MultipartFile pic) {
        String picUrl = UploadUtil.upload(pic, dir);
        return "/" + picUrl;
    }

    @RequestMapping("/commentAdd")
    @RequiredLogin
    @ResponseBody
    public Object commentAdd(ScenicComment comment, @LoginUser UserInfo user, Integer type) {
        AjaxResult result = new AjaxResult();
        //先判断用户是否登录
        if (user == null) {
            result.mark("请先登录");
            result.setCode(102);
            return result;
        }

        comment.setUserId(user.getId());
        comment.setUsername(user.getNickname());
        comment.setLevel(user.getLevel());
        comment.setHeadUrl(user.getHeadImgUrl());
        result = scenicCommentService.save(comment);

        return result;
    }


    @RequestMapping("commentReply")
    @ResponseBody
    public AjaxResult commentReply(ScenicCommentReply commentReply, @LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        //先判断用户是否登录
        if (user == null) {
            result.mark("请先登录");
            return result;
        }
        //直接回复大评论
        commentReply.setUserId(user.getId());
        commentReply.setUsername(user.getNickname());
        commentReply.setHeadUrl(user.getHeadImgUrl());
        result = replyService.save(commentReply);

        return result;
    }

    @RequestMapping("comment")
    public String listComment(@LoginUser UserInfo user,  Model model, @ModelAttribute("qo") ScenicQueryObject qo) {
        Page<ScenicComment> page = scenicCommentService.query(qo);
        model.addAttribute("page", page);
        model.addAttribute("user", user);
        return "scenic/commentTpl";
    }

    @RequestMapping("thumbup")
    @ResponseBody
    public AjaxResult thumbup(String commentId, @LoginUser UserInfo user){
        AjaxResult result = new AjaxResult();
        //先判断用户是否登录
        if (user == null) {
            result.mark("请先登录");
            return result;
        }
        result = scenicCommentService.thumbup(commentId, user.getId());

        return result;
    }

}
